package com.simple.mapper;

import com.simple.pojo.Amount;
import com.simple.pojo.Bank;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface BankMapper extends Mapper<Bank> {

    @Delete("<script> delete from tb_bank where id in " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')' >" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    void deleteByIds(@Param("ids") List<Integer> ids);

    @Select("select t3.* from " +
            "(" +
            "select user_id,bank_type,max(create_time) as create_time FROM (select * from tb_bank where user_id = #{userId}) t1 " +
            "GROUP BY user_id,bank_type ORDER BY create_time" +
            ") t2 " +
            "left join " +
            "tb_bank t3 " +
            "on t2.user_id = t3.user_id " +
            "AND t2.bank_type = t3.bank_type " +
            "and t2.create_time =t3.create_time")
    List<Bank> getLatestBanksByUserId(Integer userId);

    @Insert("<script> insert into tb_bank (user_id,amount_id,bank_type,money,create_time,create_user,update_time,update_user,remark) values" +
            "<foreach collection='batchInsertBanks' item='item'  separator=',' >" +
            "( #{item.userId},#{item.amountId},#{item.bankType},#{item.money},#{item.createTime},#{item.createUser},#{item.updateTime},#{item.updateUser},#{item.remark} )" +
            "</foreach>" +
            "</script>")
    void batchInsert(@Param("batchInsertBanks") List<Bank> batchInsertBanks);

    @Delete("delete from tb_bank where amount_id = #{amountId} and user_id = #{userId}")
    void deleteIdsByUserIdAndAmountId(@Param("amountId") Integer amoundId, @Param("userId") Integer userId);

    @Select("<script> select * from tb_bank where id in " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')' >" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Bank> getBanksByIds(List<Integer> ids);
}